package modeloDAO;

import java.util.ArrayList;
import java.util.List;
import modelo.Evento;
import hibernate.config.Sesion;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;


public class DaoEvento {
	
	
	private Sesion sesionPostgres;
					
	public void agregarEvento(Evento dato) throws Exception{
			@SuppressWarnings("static-access")
			Session em = sesionPostgres.openSession();  
	         Transaction tx = null;  
	         try {    
	        	 tx = em.beginTransaction();
	              em.save( dato);   
	              tx.commit();  
	         } catch (Exception e) {  
	             tx.rollback();            
	             e.printStackTrace();
	             throw e;
	         } finally {  
	             em.close();  
	         } 
		}
		
		public Evento obtenerEvento(int id) throws Exception{		 
			    @SuppressWarnings("static-access")
				Session sesion = sesionPostgres.openSession();  
			    Evento dato = null;        
		            try{
		                dato = (Evento) sesion.get(Evento.class,  id);
		            } catch (Exception e) {  
		            e.printStackTrace();
		           
		            throw new Exception(e.getMessage(),e.getCause());
		            }  finally {  
		                sesion.close();  
		            }  
		           
			    return dato;
		}
		
		
		public void eliminarEvento(int posi, Evento dato) throws Exception{		 
			@SuppressWarnings("static-access")
			Session sesion = sesionPostgres.openSession();  
	        Transaction tx = null;  
	        try {  
	            tx = sesion.beginTransaction();  
	            sesion.delete(dato);  
	            tx.commit();  
	           
	        } catch (Exception e) {  
	            tx.rollback();  
	           
	            throw new Exception(e.getMessage(), e.getCause());
	        } finally {  
	            sesion.close();  
	        }  
	   }
		
		public void actualizarEvento(int posi, Evento dato) throws Exception{
			@SuppressWarnings("static-access")
			Session em = sesionPostgres.openSession();  
	         Transaction tx = null;  
	         try {    
	        	 tx = em.beginTransaction();
	              em.update(dato);   
	              tx.commit();  
	         } catch (Exception e) {  
	             tx.rollback();            
	             e.printStackTrace();
	             throw e;
	         } finally {  
	             em.close();  
	         } 
		}
		
		public List<Evento> obtenerTodos() throws Exception {            
	      
			List<Evento> datos = new ArrayList<Evento>();  
		  Session em = sesionPostgres.openSession();  	
	        try {  	
		    datos =  (List<Evento>) em.createCriteria(Evento.class).list();             
	        } catch (Exception e) {             
	       
	         throw new Exception(e.getMessage(),e.getCause());
	        } finally {  
	          em.close();  
	        } 
	       
	        return datos; 
		}	
		


}
